﻿using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using RotovapPlatform.Domain.Services.Data;

namespace RotovapPlatform.UI.ViewModels.DataRecord
{
	/// <summary>
	/// 设备报警记录视图模型
	/// </summary>
	public partial class DeviceAlarmRecordViewModel : ObservableObject
	{
		private readonly ILogger<DeviceAlarmRecordViewModel> _logger;

		/// <summary>
		/// 报警记录列表
		/// </summary>
		[ObservableProperty]
		private ObservableCollection<AlarmRecord> _alarmRecords = new ObservableCollection<AlarmRecord>();

		/// <summary>
		/// 选中的报警记录
		/// </summary>
		[ObservableProperty]
		private AlarmRecord _selectedAlarmRecord;

		/// <summary>
		/// 开始日期
		/// </summary>
		[ObservableProperty]
		private DateTime _startDate = DateTime.Now.AddDays(-7);

		/// <summary>
		/// 结束日期
		/// </summary>
		[ObservableProperty]
		private DateTime _endDate = DateTime.Now;

		/// <summary>
		/// 报警类型列表
		/// </summary>
		[ObservableProperty]
		private ObservableCollection<string> _alarmTypes = new ObservableCollection<string>();

		/// <summary>
		/// 选中的报警类型
		/// </summary>
		[ObservableProperty]
		private string _selectedAlarmType;


		///// <summary>
		///// 构造函数
		///// </summary>
		///// <param name="logger">日志记录器</param>
		//public DeviceAlarmRecordViewModel(ILogger<DeviceAlarmRecordViewModel> logger)
		//{
		//	_logger = logger;
		//	_logger.LogInformation("设备报警记录视图模型已初始化");

		//	// 初始化命令
		//	SearchCommand = new RelayCommand(async () => await SearchAlarmsAsync());
		//	ExportCommand = new RelayCommand(ExportAlarms, CanExportAlarms);
		//	ClearFiltersCommand = new RelayCommand(ClearFilters);
		//}

		/// <summary>
		/// 搜索命令
		/// </summary>
		public ICommand SearchCommand { get; }

		/// <summary>
		/// 导出命令
		/// </summary>
		public ICommand ExportCommand { get; }

		/// <summary>
		/// 清除筛选条件命令
		/// </summary>
		public ICommand ClearFiltersCommand { get; }

		/// <summary>
		/// 初始化
		/// </summary>
		//public async Task InitializeAsync()
		//{
		//	try
		//	{
		//		IsLoading = true;
		//		_logger.LogInformation("正在初始化设备报警记录...");

		//		// 初始化报警类型
		//		AlarmTypes.Clear();
		//		AlarmTypes.Add("全部");
		//		AlarmTypes.Add("温度异常");
		//		AlarmTypes.Add("压力异常");
		//		AlarmTypes.Add("旋转异常");
		//		AlarmTypes.Add("通信异常");
		//		AlarmTypes.Add("其他");

		//		SelectedAlarmType = "全部";

		//		// 这里将来会添加实际的数据加载逻辑
		//		await Task.Delay(500); // 模拟加载延迟

		//		// 模拟添加一些报警记录
		//		await SearchAlarmsAsync();

		//		_logger.LogInformation("设备报警记录初始化完成");
		//	}
		//	catch (Exception ex)
		//	{
		//		_logger.LogError(ex, "初始化设备报警记录时发生错误");
		//	}
		//	finally
		//	{
		//		IsLoading = false;
		//	}
		//}

		///// <summary>
		///// 搜索报警记录
		///// </summary>
		//private async Task SearchAlarmsAsync()
		//{
		//	try
		//	{
		//		IsLoading = true;
		//		_logger.LogInformation($"正在搜索从 {StartDate:yyyy-MM-dd} 到 {EndDate:yyyy-MM-dd} 的报警记录...");

		//		// 这里将来会添加实际的搜索逻辑
		//		await Task.Delay(500); // 模拟搜索延迟

		//		// 清空现有记录
		//		AlarmRecords.Clear();

		//		// 模拟添加一些报警记录
		//		var random = new Random();
		//		var alarmTypes = new[] { "温度异常", "压力异常", "旋转异常", "通信异常", "其他" };
		//		var deviceComponents = new[] { "油浴", "冷凝器", "旋转电机", "真空泵", "控制器" };
		//		var severities = new[] { "低", "中", "高", "紧急" };

		//		for (int i = 0; i < 20; i++)
		//		{
		//			var alarmTime = StartDate.AddDays(random.NextDouble() * (EndDate - StartDate).TotalDays);
		//			var alarmType = alarmTypes[random.Next(alarmTypes.Length)];

		//			// 如果选择了特定报警类型且不是"全部"，则只添加该类型的报警
		//			if (SelectedAlarmType != "全部" && alarmType != SelectedAlarmType)
		//			{
		//				continue;
		//			}

		//			AlarmRecords.Add(new AlarmRecord
		//			{
		//				Id = $"ALM-{DateTime.Now.Year}{DateTime.Now.Month:D2}{i + 1:D3}",
		//				Time = alarmTime,
		//				Type = alarmType,
		//				Component = deviceComponents[random.Next(deviceComponents.Length)],
		//				Description = $"{alarmType}：{deviceComponents[random.Next(deviceComponents.Length)]}参数超出正常范围",
		//				Severity = severities[random.Next(severities.Length)],
		//				IsHandled = random.Next(2) == 0
		//			});
		//		}

		//		_logger.LogInformation($"搜索到 {AlarmRecords.Count} 条报警记录");
		//	}
		//	catch (Exception ex)
		//	{
		//		_logger.LogError(ex, "搜索报警记录时发生错误");
		//	}
		//	finally
		//	{
		//		IsLoading = false;
		//	}
		//}

		///// <summary>
		///// 导出报警记录
		///// </summary>
		//private void ExportAlarms()
		//{
		//	try
		//	{
		//		_logger.LogInformation("正在导出报警记录...");

		//		// 这里将来会添加实际的导出逻辑

		//		_logger.LogInformation("报警记录导出完成");
		//	}
		//	catch (Exception ex)
		//	{
		//		_logger.LogError(ex, "导出报警记录时发生错误");
		//	}
		//}

		///// <summary>
		///// 是否可以导出报警记录
		///// </summary>
		//private bool CanExportAlarms()
		//{
		//	return AlarmRecords.Count > 0;
		//}

		///// <summary>
		///// 清除筛选条件
		///// </summary>
		//private void ClearFilters()
		//{
		//	StartDate = DateTime.Now.AddDays(-7);
		//	EndDate = DateTime.Now;
		//	SelectedAlarmType = "全部";
		//}
	}

	/// <summary>
	/// 报警记录类
	/// </summary>
	public class AlarmRecord
	{
		/// <summary>
		/// 报警ID
		/// </summary>
		public string Id { get; set; }

		/// <summary>
		/// 报警时间
		/// </summary>
		public DateTime Time { get; set; }

		/// <summary>
		/// 报警类型
		/// </summary>
		public string Type { get; set; }

		/// <summary>
		/// 报警组件
		/// </summary>
		public string Component { get; set; }

		/// <summary>
		/// 报警描述
		/// </summary>
		public string Description { get; set; }

		/// <summary>
		/// 报警严重程度
		/// </summary>
		public string Severity { get; set; }

		/// <summary>
		/// 是否已处理
		/// </summary>
		public bool IsHandled { get; set; }
	}
}